{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n"
     ]
    }
   ],
   "source": [
    "l1 = []\n",
    "for i in range(1,11):\n",
    "    l1.append(i+i)\n",
    "print(l1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法二"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n"
     ]
    }
   ],
   "source": [
    "l2 = [x+x for x in range(1,11)]\n",
    "print(l2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "方法二中只需要一行代码就实现了方法一中的功能，方法二采用的便是列表生成式；\n",
    "\n",
    "**写列表生成式时，需要把表达式：x+x 写在最前面，后面再跟 for 循环；**\n",
    "\n",
    "### 每次循环多个变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['x=1', 'y=2', 'z=3']\n"
     ]
    }
   ],
   "source": [
    "d = {'x': '1','y': '2','z': '3'}\n",
    "l5 = [k + '=' + v for k,v in d.items()]\n",
    "print(l5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### for循环加if判断\n",
    "\n",
    "列表生成式中 if……else 的用法\n",
    "\n",
    "1. for 后面加 if 是一个筛选条件，不能带 else\n",
    "2. if 在 for 前面必须加 else，因为 for 前面是一个表达式\n",
    "（在列表生成式中，for 前面的 if……else 是表达式，for 后面的 if 是过滤条件，不能带 else）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 8, 12, 16, 20]\n"
     ]
    }
   ],
   "source": [
    "l3 = [x+x for x in range(1,11) if x % 2 == 0]\n",
    "print(l3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1, 2, -3, 4, -5, 6, -7, 8, -9, 10]\n"
     ]
    }
   ],
   "source": [
    "l4 = [x if x % 2 == 0 else -x for x in range(1,11)]\n",
    "print(l4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "循环加多条件判断，只是表达式复杂一点，且只能在 for 前面, **三元条件表达式需要从左往右判读**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['El', 'El', 'Em', 'None']\n"
     ]
    }
   ],
   "source": [
    "params = ['Ellipsis', 'EllipsisEmpty', 'Empty', 'None']\n",
    "\n",
    "print(['El' if \"Ellipsis\" in a else 'Em' if \"Empty\" in a else a for a in params])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "等价于"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['El', 'El', 'Em', 'None']\n"
     ]
    }
   ],
   "source": [
    "l7 = []\n",
    "for i in params:\n",
    "    if \"Ellipsis\" in i:\n",
    "        l7.append('El')\n",
    "    else:\n",
    "        if \"Empty\" in i:\n",
    "            l7.append('Em')\n",
    "        else:\n",
    "            l7.append(i)\n",
    "print(l7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多层for循环\n",
    "\n",
    "循环体**从左往右**执行，右侧全部循环完后再从左侧取一个元素，继续右侧的全部循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']\n"
     ]
    }
   ],
   "source": [
    "l5 = [x+y for x in '123' for y in 'ABC' ]\n",
    "print(l5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "等价于："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1A', '1B', '1C', '2A', '2B', '2C', '3A', '3B', '3C']\n"
     ]
    }
   ],
   "source": [
    "l5 = []\n",
    "for x in '123':\n",
    "    for y in 'ABC':\n",
    "        l5.append(x+y)\n",
    "print(l5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字典生成式\n",
    "\n",
    "外层用花括号，里面的第一个表达式是冒号分开的键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'xx': 2, 'yy': 4, 'zz': 6}\n"
     ]
    }
   ],
   "source": [
    "d = {'x': 1, 'y': 2, 'z': 3}\n",
    "l6 = {k * 2: v * 2 for k, v in d.items()}\n",
    "print(l6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 集合生成式\n",
    "\n",
    "最外层是花括号即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4, 5}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{abs(i) for i in range(-5, 5)}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 生成器\n",
    "\n",
    "最外层是小括号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object <genexpr> at 0x000002837575BC78>\n",
      "[0, 2, 4]\n"
     ]
    }
   ],
   "source": [
    "gen = (i for i in range(5) if i % 2 == 0)\n",
    "print(gen)\n",
    "print(list(gen))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "171.273px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
